package de.bazinga.rotw.objects;

import java.net.URL;

import com.jme.bounding.BoundingBox;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jmex.jbullet.PhysicsSpace;
import com.jmex.jbullet.collision.shapes.CollisionShape;
import com.jmex.jbullet.nodes.PhysicsNode;

import de.bazinga.rotw.collision.CollidableObjects;
import de.bazinga.rotw.texture.TextureCache;

public class Stair extends Node {
	private static final long serialVersionUID = 1L;
	
	public static final int XDIRECTIONUP = 1;
	public static final int XDIRECTIONDOWN = 2;
	public static final int ZDIRECTIONUP = 3;
	public static final int ZDIRECTIONDOWN = 4;
	
	public Stair(Vector3f start, Vector3f end, int steps, URL texture, int dirFlag) {
		super("Stairs");
		

		float heightPerStep = (end.getY() - start.getY()) / (float)steps;
		float depthPerStep = 0;
		if(dirFlag == 1 || dirFlag == 2) {
			depthPerStep = (end.getX() - start.getX()) / (float)steps;
		}else{
			depthPerStep = (end.getZ() - start.getZ()) / (float)steps;
		}
			
		Box stepBox = null;
		float startX = 0, startY = 0, startZ = 0, endX = 0, endY = 0, endZ = 0;
		for(int i=0;i < steps; i++) {
			
			if(dirFlag == 1) {
				// start.x is the lowest point and it goes up to end.x
				startX = start.getX() + (i * depthPerStep);
				startY = start.getY()+ (i * heightPerStep);
				startZ = start.getZ();
				
				endX = end.getX();
				endY = start.getY()+ (i * heightPerStep) + heightPerStep;
				endZ = end.getZ();
			}else if(dirFlag == 2) {
				// start.x is the hightest point and it goes down to end.x
				startX = start.getX();
				startY = start.getY() + (i * heightPerStep);
				startZ = start.getZ();
				
				endX = end.getX() - (i * depthPerStep);
				endY = start.getY()+ (i * heightPerStep) + heightPerStep;
				endZ = end.getZ();
			}else if(dirFlag == 3) {
				// start.z is the lowest point and it goes up to end.z
				startX = start.getX();
				startY = start.getY() + (i * heightPerStep);
				startZ = start.getZ() + (i * depthPerStep);
				
				endX = end.getX();
				endY = start.getY() + (i * heightPerStep) + heightPerStep;
				endZ = end.getZ();
			}else if(dirFlag == 4) {
				// start.z is the highest point and it goes down to end.z
				startX = start.getX();
				startY = start.getY() + (i * heightPerStep);
				startZ = start.getZ();
				
				endX = end.getX();
				endY = start.getY() + (i * heightPerStep) + heightPerStep;
				endZ = end.getZ() - (i * depthPerStep);
			}
			
			Vector3f min = new Vector3f(startX, startY, startZ);
			Vector3f max = new Vector3f(endX, endY, endZ);
			
			Vector3f extend = max.subtract(min).mult(0.5f);
			stepBox = new Box("step",Vector3f.ZERO,extend.x, extend.y, extend.z);
			
			
			/*stepBox = new Box("step",
					new Vector3f(startX, startY, startZ),
					new Vector3f(endX, endY, endZ));
			*/
			
			stepBox.setModelBound(new BoundingBox());
			stepBox.updateModelBound();
			
			stepBox.setRenderState(TextureCache.getInstance().getTexture(texture));
			stepBox.updateRenderState();
			
			// Create PhysicsNode
			PhysicsNode stepPhysicsNode = new PhysicsNode(stepBox,CollisionShape.ShapeTypes.BOX,0);
			stepPhysicsNode.setLocalTranslation(min.add(extend));				
			
			
			this.attachChild(stepPhysicsNode);

			stepPhysicsNode.updateRenderState();
			PhysicsSpace.getPhysicsSpace().add(stepPhysicsNode);
			
			CollidableObjects.getInstance().addObject("wall",stepBox);
		}
	}
}
